package com.maybach.core.template.impl;

import com.maybach.core.template.ITemplate;
import com.maybach.core.template.ITemplateService;
import com.maybach.core.template.TemplateFactory;
import com.maybach.workflow.core.expression.IExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by houenxun on 16/3/7.
 */
@Component
public class TemplateService implements ITemplateService {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    IExpression velocityExpression;

    @Override
    public String getTempateBody(String target, Map<String, Object> params) {
        ITemplate template = TemplateFactory.getTemplate(target);
        if (null != template) {
            if (template.checkParams(params)) {
                return velocityExpression.eval(String.class, template.getTemplate(), params);
            }
        }
        return null;
    }

    @Override
    public String getTemplateTitle(String target) {
        ITemplate template = TemplateFactory.getTemplate(target);
        if (null != template) {
            return template.getTitle();
        }
        return null;
    }
}
